package com.juxing.sdk.util;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.juxing.sdk.SdkException;

/**
 * 加密工具
 * @author brochen
 * @mail mongibello@163.com
 * @created 2016年2月1日
 *
 */
public class DigestUtil {
	
	//算法名称  
	private static final String ALGORITHM = "DESede";  
	//
	private static final char HEX_DIGITS[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	
	/**
	 * 加密(3DES)
	 * @param src 明文
	 * @param keybyte 密钥
	 * @return
	 */
	public static String encrypt3Des(String src, byte[] keybyte) {
		if(StringUtil.isEmpty(src) || keybyte == null) {
			throw new SdkException("invalid parameter");
		}
		try {
			SecretKey deskey = new SecretKeySpec(keybyte, ALGORITHM);
		    Cipher cipher = Cipher.getInstance(ALGORITHM);  
		    cipher.init(Cipher.ENCRYPT_MODE, deskey);  
		    byte[] b = src.getBytes();
		    byte[] ret = cipher.doFinal(b);  
//		    BASE64Encoder encoder = new BASE64Encoder();  
//		    return encoder.encode(ret);  
		    return StringUtil.byte2hex(ret);
		} catch (Exception e) {
			throw new SdkException("error occurated when encrypt", e);
		}
	}
	/**
	 * 解密(3DES)
	 * @param src 密文 
	 * @param keybyte 密钥
	 * @return
	 */
	public static String decrypt3Des(String src, byte[] keybyte) {
		if(StringUtil.isEmpty(src) || keybyte == null) {
			throw new SdkException("invalid parameter");
		}
		try {
			SecretKey deskey = new SecretKeySpec(keybyte, ALGORITHM);
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, deskey);
//			BASE64Decoder decoder = new BASE64Decoder();  
//			byte[] b1 = decoder.decodeBuffer(src); 
			byte[] b1 = StringUtil.hex2byte(src);
			/** 执行解密操作 */  
			byte[] b = cipher.doFinal(b1);  
			return new String(b);
		} catch (Exception e) {
			throw new SdkException("error occurated when decrypt", e);
		}
	}
	
	/**
	 * 
	 * Title:
	 * Description:md5加密
	 * @Create_by:JH
	 * @Create_date:2014-9-9
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:exinhua 1.0
	 */
	public static String encryptMd5(String src) {
		if(StringUtil.isEmpty(src)) {
			return src;
		}
		try {
			byte[] btInput = src.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
                str[k++] = HEX_DIGITS[byte0 & 0xf];
            }
            return new String(str);
		} catch (Exception e) {
			throw new SdkException("error occurated when encrypt", e);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(encryptMd5("1"));
		System.out.println(encryptMd5("2").length());
		System.out.println(encryptMd5(encryptMd5("123456") + encryptMd5("kjhytd")));
	}
}
